package com.cow.framework.core.login;

import org.springframework.stereotype.Service;

import com.cow.framework.core.base.BaseComponent;
import com.cow.framework.core.config.constant.MessageConstants;
import com.cow.framework.core.exception.BusinessException;
import com.cow.framework.utils.encrypt.MD5Util;
import com.cow.sram.data.dto.User;
import com.cow.sram.remote.AuthRemoteService;


/**
 * 登录服务
 * 
 * @author yangsj
 * */
@Service
public class LoginService extends BaseComponent{
	
	/**登录*/
	public User login(String accountName,String password,String checkCode) {		
		AuthRemoteService client = super.getAuthClient();
		User user = client.loadUserByAccountname(accountName, appName);
		if(user == null){
			throw new BusinessException("B1010",MessageConstants.B1010);
		}
		
		if(!active.equals("dev") && !active.equals("local")){//开发模式不做校验
			//用户名 +密码+验证码再次加密后比较
			String validCode = MD5Util.crypt(user.getAccountname() + user.getPassword() + checkCode);
			if(!validCode.equals(password)){
				throw new BusinessException("B1010",MessageConstants.B1010);
			}			
		}	
		
		if(user.getLockflag().equals("1")){
			throw new BusinessException("B1015",MessageConstants.B1015);
		}
		
		if(user.getResourceMap().get("t0")== null){
			throw new BusinessException("B1014",MessageConstants.B1014);
		}
				
		return user;
	}
}
